package com.eqdd.yiqidian.ui.companyspace

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.BaseActivity
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.ProductDemandBean
import com.eqdd.yiqidian.box.ItemDecoration.DividerLineItemDecoration
import com.eqdd.yiqidian.http.DialogCallBack
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.http.HttpResult
import com.eqdd.yiqidian.kotlin.BaseRecyclerViewAdapter
import com.eqdd.yiqidian.kotlin.deleteDialog
import com.eqdd.yiqidian.kotlin.toArrayList
import com.eqdd.yiqidian.utils.TimeUtil
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_recyclerview.*
import kotlinx.android.synthetic.main.list_item_167.view.*
import org.jetbrains.anko.sdk25.coroutines.onClick
import org.jetbrains.anko.startActivityForResult
import org.jetbrains.anko.toast

/**
 * Created by jhj on 18-7-21.
 */
class ProductSupplierListActivity : BaseActivity() {

    private var productId: Int = -1
    private val adapter = ListAdapter()
    private var type: Int? = null
    private var deleteUrl: String = ""
    private var demandUrl: String = ""
    private var function: Int = -1


    override fun initBinding() {
        setContentView(R.layout.activity_recyclerview)
        productId = intent.getIntExtra(Config.ID, -1)
        type = intent.getIntExtra(Config.TYPE, -1)
        function = intent.getIntExtra("fun", -1)

        if (productId == -1 || type == -1 && function == -1) {
            toast("获取供方需求失败")
            finish()
            return
        }


        val title = if (type == 0) {
            "供方信息列表"
        } else if (type == 1) {
            "买方信息列表"
        } else {
            "信息列表"
        }

        initTopTitleBar(View.VISIBLE, title)
        if (function == 0) {
            initTopRightText("新增") {
                startActivityForResult<ProductSupplierAddActivity>(Config.ADD, Config.TYPE to type, Config.ID to productId)
            }
        }
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(DividerLineItemDecoration(this))
        recyclerView.adapter = adapter
    }


    override fun initData() {

        if (type == 0) {
            deleteUrl = HttpConfig.BASE_URL + HttpConfig.SUPPLY_DELETE
            demandUrl = HttpConfig.BASE_URL + HttpConfig.SUPPLY_GET
        } else if (type == 1) {
            deleteUrl = HttpConfig.BASE_URL + HttpConfig.BUYER_DELETE
            demandUrl = HttpConfig.BASE_URL + HttpConfig.BUYER_GET
        } else {
            toast("获取信息失败")
            finish()
        }

        OkGo.post<HttpResult<List<ProductDemandBean>>>(demandUrl)
                .params("productId", productId)
                .execute(object : DialogCallBack<HttpResult<List<ProductDemandBean>>>(this) {
                    override fun onSuccess(response: Response<HttpResult<List<ProductDemandBean>>>?) {
                        val result = response?.body()
                        if (result?.status == 200 && result.items != null) {
                            adapter.dataList = result.items.toArrayList()
                            adapter.notifyDataSetChanged()
                        } else {
                            toast(result?.msg ?: "获取供方需求失败")
                            finish()
                        }
                    }

                })
    }

    override fun setView() {

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && requestCode == Config.ADD) {
            initData()
        }
    }

    inner class ListAdapter : BaseRecyclerViewAdapter<ProductDemandBean>() {
        override fun onCreateView(viewType: Int): Int {
            return R.layout.list_item_167
        }

        override fun onBindViewItemViewHolder(holder: ItemViewHolder, bean: ProductDemandBean, position: Int) {
            holder.itemView?.let {
                it.tv_content.text = bean.productName
                it.tv_time.text = TimeUtil.formatWebTime1(bean.createTime)
                it.onClick {
                    if (type == 0) {
                        startActivityForResult<ProDemandInfoActivity>(Config.UPDATE, Config.ID to bean.id, Config.TYPE to 0, "fun" to function)
                    } else if (type == 1) {
                        startActivityForResult<ProDemandInfoActivity>(Config.UPDATE, Config.ID to bean.id, Config.TYPE to 1, "fun" to function)
                    }
                }
                if (function == 0) {
                    it.setOnLongClickListener {
                        deleteDialog(deleteUrl, "是否删除“${bean.productName}”？", "buyerId" to bean.id, "productId" to productId, isFinish = false) {
                            initData()
                        }
                        true
                    }
                }

            }
        }

    }
}